package patrol.mobile.com.mobilepatrol.utils;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.widget.DatePicker;
import android.widget.TimePicker;

import java.util.Calendar;

import patrol.mobile.com.mobilepatrol.bean.DateBean;
import patrol.mobile.com.mobilepatrol.view.CallBack;

/**
 * Created by z on 2018/9/8.
 * Des: 日期选择工具
 */

public class DatePickerUtil02 {
    private static DatePickerUtil02 datePickerUtil;
    private CallBack<DateBean> callBack;

    public static DatePickerUtil02 getInstance(Context context) {
//        if (datePickerUtil == null) {
        datePickerUtil = new DatePickerUtil02(context);
//        }
        return datePickerUtil;
    }

    private DatePickerUtil02(final Context context) {
        final Calendar c = Calendar.getInstance();
        int mYear = c.get(Calendar.YEAR);
        int mMonth = c.get(Calendar.MONTH);
        int mDay = c.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog datePickerDialog = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, final int year, final int month, final int dayOfMonth) {
                TimePickerDialog timePickerDialog = new TimePickerDialog(view.getContext(), new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        DateBean dateBean = new DateBean();
                        dateBean.setmYear(year);
                        String yuefen = (month + 1) + "";
                        dateBean.setmMonth(yuefen);
                        dateBean.setmDay(dayOfMonth);
                        String tianshu = dayOfMonth + "";
                        String xiaoshi = hourOfDay + "";
                        String fenzhong = minute + "";

                        if (dayOfMonth < 10) {
                            tianshu = "0" + dayOfMonth;
                        }
                        if (month < 9) {
                            yuefen = "0" + (month + 1);
                        }
                        if (hourOfDay < 9) {
                            xiaoshi = "0" + (hourOfDay );
                        }
                        if (minute < 9) {
                            fenzhong = "0" + (minute);
                        }
                        dateBean.setmDate(year + "-" + yuefen + "-" + tianshu+" "+xiaoshi+":"+fenzhong+":00");
                        callBack.onBack(dateBean);
                    }
                }, c.get(Calendar.HOUR_OF_DAY)
                        , c.get(Calendar.MINUTE)
                        // true表示采用24小时制
                        ,true);
                timePickerDialog.show();
            }
        }, mYear, mMonth, mDay);
        datePickerDialog.show();
    }

    public void setCallBack(CallBack<DateBean> callBack) {
        this.callBack = callBack;
    }
}
